/*******************************************************************************
 * Copyright 2017 Bstek
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.bstek.ureport.console.importexcel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bstek.ureport.console.RenderPageServletAction;
import com.bstek.ureport.console.cache.TempObjectCache;
import com.bstek.ureport.console.designer.DesignerServletAction;
import com.bstek.ureport.definition.ReportDefinition;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;
import org.hzero.report.domain.repository.ReportRepository;
import org.hzero.report.infra.util.UReportUtils;

import io.choerodon.core.convertor.ApplicationContextHelper;

/**
 * @author Jacky.gao
 * @since 2017年5月25日
 */
public class ImportExcelServletAction extends RenderPageServletAction {

    private volatile static ReportRepository reportRepository;

    private ReportRepository getReportRepository() {
        if (null == reportRepository) {
            synchronized (DesignerServletAction.class) {
                if (null == reportRepository) {
                    reportRepository = ApplicationContextHelper.getContext().getBean(ReportRepository.class);
                }
            }
        }
        return reportRepository;
    }

    private List<ExcelParser> excelParsers = new ArrayList<ExcelParser>();

    public ImportExcelServletAction() {
        excelParsers.add(new HSSFExcelParser());
        excelParsers.add(new XSSFExcelParser());
    }

    @Override
    public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tempDir = System.getProperty("java.io.tmpdir");
        FileItemFactory factory = new DiskFileItemFactory(1000240, new File(tempDir));
        ServletFileUpload upload = new ServletFileUpload(factory);
        ReportDefinition report = null;
        String errorInfo = null;
        try {
            List<FileItem> items = upload.parseRequest(req);
            for (FileItem item : items) {
                String fieldName = item.getFieldName();
                String name = item.getName().toLowerCase();
                if (fieldName.equals("_excel_file") && (name.endsWith(".xls") || name.endsWith(".xlsx"))) {
                    InputStream inputStream = item.getInputStream();
                    for (ExcelParser parser : excelParsers) {
                        if (parser.support(name)) {
                            report = parser.parse(inputStream);
                            break;
                        }
                    }
                    inputStream.close();
                    break;
                }
            }
            errorInfo = "请选择一个合法的Excel导入";
        } catch (Exception e) {
            e.printStackTrace();
            errorInfo = e.getMessage();
        }
        Map<String, Object> result = new HashMap<String, Object>();
        if (report != null) {
            result.put("result", true);
            TempObjectCache.putObject("classpath:template/template.ureport.xml", report);

            // 记录报表uuid和报表定义
            String uuid = UReportUtils.getParamsWithQueryString(req.getQueryString(), "uuid");
            if (StringUtils.isNotBlank(uuid)) {
                UReportUtils.addCache(uuid, report);
            }
        } else {
            result.put("result", false);
            if (errorInfo != null) {
                result.put("errorInfo", errorInfo);
            }
        }
        writeObjectToJson(resp, result);
    }

    @Override
    public String url() {
        return "/import";
    }
}
